﻿@using System.Configuration
@using System.Data.SqlClient
@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Export Claim Page";

    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    string sql_filter = string.Empty;
    string frm_filter_claimstartdate = string.Empty;
    string frm_filter_claimenddate = string.Empty;
    string frm_filter_claimstatus = string.Empty;
    DateTime tempDate;
    
    System.Data.DataSet ds = new System.Data.DataSet("CurrentClaims");
    List<KeppelLand.Entities.Claim> claimList = null;
    KeppelLand.Entities.ClaimSummary claimSummary = null;
    List<KeppelLand.Entities.ClaimPeriod> claimPeriodList = null;
    
    if (IsPost)
    {
        if (Request.Form["actionButton"] == "filter")
        {
            sql_filter = string.Empty;
            
            if (!string.IsNullOrEmpty(Request.Form["claimStartDate"]))
            {
                frm_filter_claimstartdate = Request.Form["claimStartDate"].Trim();
                if (DateTime.TryParseExact(Request.Form["claimStartDate"].Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out tempDate))
                {
                    sql_filter = sql_filter + string.Format(" AND ClaimDate >= '{0}' ", tempDate.ToString("yyyy-MM-dd"));
                }
            }
            if (!string.IsNullOrEmpty(Request.Form["claimEndDate"]))
            {
                
                frm_filter_claimenddate = Request.Form["claimEndDate"].Trim();
                
                if (DateTime.TryParseExact(Request.Form["claimEndDate"].Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out tempDate))
                {
                    sql_filter = sql_filter + string.Format(" AND ClaimDate <= '{0}' ", tempDate.ToString("yyyy-MM-dd"));
                }
            }
            if (!string.IsNullOrEmpty(Request.Form["claimStatus"]))
            {
                frm_filter_claimstatus = Request.Form["claimStatus"].Trim();
                switch (frm_filter_claimstatus.ToUpperInvariant())
                {
                    case "A":
                        sql_filter = sql_filter + " AND Status = 0 ";
                        break;
                    case "B":
                        sql_filter = sql_filter + " AND (Status = 1 OR Status = 3) ";
                        break;
                    case "C":
                        sql_filter = sql_filter + " AND (Status = 5 OR Status = 6) ";
                        break;
                }
            }
        } // if (Request.Form["actionButton"] == "filter")
        
        if (Request.Form["actionButton"] == "download")
        {
            sql_filter = string.Empty;

            if (!string.IsNullOrEmpty(Request.Form["claimStartDate"]))
            {
                frm_filter_claimstartdate = Request.Form["claimStartDate"].Trim();
                if (DateTime.TryParseExact(Request.Form["claimStartDate"].Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out tempDate))
                {
                    sql_filter = sql_filter + string.Format(" AND ClaimDate >= '{0}' ", tempDate.ToString("yyyy-MM-dd"));
                }
            }
            if (!string.IsNullOrEmpty(Request.Form["claimEndDate"]))
            {

                frm_filter_claimenddate = Request.Form["claimEndDate"].Trim();

                if (DateTime.TryParseExact(Request.Form["claimEndDate"].Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out tempDate))
                {
                    sql_filter = sql_filter + string.Format(" AND ClaimDate <= '{0}' ", tempDate.ToString("yyyy-MM-dd"));
                }
            }
            if (!string.IsNullOrEmpty(Request.Form["claimStatus"]))
            {
                frm_filter_claimstatus = Request.Form["claimStatus"].Trim();
                switch (frm_filter_claimstatus.ToUpperInvariant())
                {
                    case "A":
                        sql_filter = sql_filter + " AND Status = 0 ";
                        break;
                    case "B":
                        sql_filter = sql_filter + " AND (Status = 1 OR Status = 3) ";
                        break;
                    case "C":
                        sql_filter = sql_filter + " AND (Status = 5 OR Status = 6) ";
                        break;
                }
            }
            
            byte[] fileBytes = KeppelLand.Controllers.ReportController.GetClaimHistoryReport(
                Request.Form["download_format"],
                KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), sql_filter);
            Response.Clear();
            Response.ContentType = KeppelLand.Helpers.TranslationHelper.GetContentType(Request.Form["download_format"]);
            Response.AddHeader("content-disposition", "attachment; filename=" + KeppelLand.Helpers.TranslationHelper.GetFileName(Request.Form["download_format"], "Claim-History"));
            Response.BinaryWrite(fileBytes);
            Response.Flush();
            Response.End();
        } // if (Request.Form["actionButton"] == "download")
    } // if (IsPost)

    claimList = KeppelLand.Controllers.ClaimController.GetClaimList(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), sql_filter);
    claimSummary = KeppelLand.Controllers.ClaimController.GetClaimSummary(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName());
    claimPeriodList = KeppelLand.Controllers.ClaimController.GetClaimPeriodList(KeppelLand.Helpers.UserIdentityHelper.GetClaimantName(), KeppelLand.Entities.ClaimStatus.EXPORT_TO_SAP);
}

<div class="container-fluid main-content" role="main">

    @RenderPage("~/Shared/Help_Tab.cshtml")

    @if (hasError)
    {
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @Html.Raw(errorMessage)
        </div>
    }
    @if (hasSuccess)
    {
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            @successMessage
        </div>
    }
    
    <form method="post" id="filter_form">
        <h2>Claim History</h2>

        <table class="table table-striped table-bordered">
            <tbody>
                <tr>
                    <th class="col-lg-2" style="vertical-align:middle;">
                        Date Filter
                    </th>
                    <td class="col-lg-10">
                        <label>Start Date</label>
                        <input type="date" class="datepicker" name="claimStartDate" id="claimStartDate" style="width:116px;" value="@frm_filter_claimstartdate" autocomplete="off" />
                        &nbsp;&nbsp;
                        <label>End Date</label>
                        <input type="date" class="datepicker" name="claimEndDate" id="claimEndDate" style="width:116px;" value="@frm_filter_claimenddate" autocomplete="off" />
                    </td>
                    <td rowspan="2" style="vertical-align:middle">
                        <button type="button" class="btn btn-primary" name="filterButton" id="filterButton">Refresh</button>
                    </td>
                </tr>
                <tr>
                    <th class="col-lg-2" style="vertical-align:middle;">
                        Status Filter
                    </th>
                    <td class="col-lg-10">
                        <select name="claimStatus" id="claimStatus">
                            <option value="" @DisplayHelper.AddSelectedAttribute(frm_filter_claimstatus == "")>All Status</option>
                            <option value="A" @DisplayHelper.AddSelectedAttribute(frm_filter_claimstatus == "A")>Pending Submission</option>
                            <option value="B" @DisplayHelper.AddSelectedAttribute(frm_filter_claimstatus == "B")>Pending Approval</option>
                            <option value="C" @DisplayHelper.AddSelectedAttribute(frm_filter_claimstatus == "C")>Processed(HR)</option>
                        </select>

                    </td>
                </tr>
            </tbody>
        </table>

        <input type="hidden" name="actionButton" id="filter_form_action" />
    </form>

    @RenderPage("~/Shared/Claims_History.cshtml", claimList, claimSummary)

    <p>&nbsp;</p>

</div>

@RenderPage("~/Shared/Standard_Modals.cshtml")

<div class="modal fade" id="messageModal" tabindex="-1" role="dialog" aria-labelledby="messageModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="messageModalLabel">Message</h4>
            </div>
            <div class="modal-body" id="messageModalBody">
                <p>
                    <strong>Rejection Reason:</strong>
                </p>
                <p id="messageModal_rejected_reason">
                    Click OK to proceed with this action.<br />
                    Click Canel to abort this action.
                </p>

                <p>
                    <strong>Rejected By</strong><br />
                    <span id="messageModal_rejected_by"></span>
                </p>
                <p>
                    <strong>Rejected Date</strong><br />
                    <span id="messageModal_rejected_date"></span>
                </p>
            </div>

            <div class="modal-footer">

                <button type="button" class="btn btn-primary" id="messageModal_OK" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="helpModalLabel">Help <small>(Claim History)</small></h4>
            </div>
            <div class="modal-body">
                <p>
                    Use this page to view claims submitted to Finance by HR .<br />
                </p>
                <!-- 
                <div class="panel panel-default">
                    <div class="panel-heading">Instructions to add new claim</div>
                    <div class="panel-body">
                        <ol>
                            <li>Enter claim date, official hours and overtime hours. These are required fields.</li>
                            <li>
                                Fill in overtime hours if applicable.<br />
                                This is required if you intend to claim OT or meal allowances.
                            </li>
                        </ol>
                    </div>
                </div>

                -->


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="helpModal_Close" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript">
    function download(format) {

        $("#approval_form #claimStartDate").val($("#filter_form #claimStartDate").val());
        $("#approval_form #claimEndDate").val($("#filter_form #claimEndDate").val());
        $("#approval_form #claimStatus").val($("#filter_form #claimStatus").val());
        $("#approval_form #actionButton").val("download");
        $("#approval_form #download_format").val(format);
        $("#approval_form").submit();
    }

    (function ($) {
        "use strict";

        var optionsDate, ctlClaimStartDate, ctlClaimEndDate;

        optionsDate = {
            format: "dd/MM/yyyy",
            parseFormats: [
            "dd/MMMM/yyyy",
            "dd-MMMMyyyy",
            "dd/MM/yyyy",
            "dd-MM-yyyy",
            "ddMMyyyy",
            "ddMMyy",
            "ddMM"
            ],
            change: function () { }
        }; // optionsDate = {

        ctlClaimStartDate = $("#claimStartDate").kendoDatePicker(optionsDate);
        ctlClaimEndDate = $("#claimEndDate").kendoDatePicker(optionsDate);

        $(document).ready(function () {
            $("#filterButton").click(function () {
                //$("#filter_entries").val(this.value());
                $("#filter_form_action").val("filter");
                $("#filter_form").submit();
            });
        });
    }(jQuery));
    </script>
}